package com.zy.ioc.pro4_ioc_factorybean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Pro4_App_Test {
    public static void main(String[] args) {
        ApplicationContext ac=new AnnotationConfigApplicationContext(  Pro4_App_Test.class   );

        String[] beanNames=ac.getBeanDefinitionNames();
        for(String bn:beanNames){
            System.out.println(bn);
        }

        //通过工厂bean的名字得到的是  工厂生产的bean对象. 而不是工厂bean本身
        // 底层是调用  getObject()方法
        Fruit f= (Fruit) ac.getBean("fruitFactoryBean");
        System.out.println(f);

        //如果真在想取工厂bean本身,需要在工厂bean的名字前面加上 &
        FruitFactoryBean ffb= (FruitFactoryBean) ac.getBean("&fruitFactoryBean");
        System.out.println(ffb);

    }

    @Bean
    public FruitFactoryBean fruitFactoryBean(){
        return new FruitFactoryBean();
    }

}
